﻿using System.Windows.Controls;
using System.Windows;

namespace ClassMetadataBrowser.UI
{
	/// <summary>
	/// Column with the fixed width
	/// </summary>
	public class FixedWidthColumn : GridViewColumn
	{
		static FixedWidthColumn()
		{
			WidthProperty.OverrideMetadata(typeof(FixedWidthColumn),
				new FrameworkPropertyMetadata(null, new CoerceValueCallback(OnCoerceWidth)));
		}

		public static readonly DependencyProperty FixedWidthProperty =
			DependencyProperty.Register("FixedWidth", typeof(double), typeof(FixedWidthColumn),
			new FrameworkPropertyMetadata(double.NaN, new PropertyChangedCallback(OnFixedWidthChanged)));

		public double FixedWidth
		{
			get { return (double)GetValue(FixedWidthProperty); }
			set { SetValue(FixedWidthProperty, value); }
		}

		private static void OnFixedWidthChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
		{
			FixedWidthColumn fwc = (FixedWidthColumn)o;
			fwc.CoerceValue(WidthProperty);
		}

		private static object OnCoerceWidth(DependencyObject o, object baseValue)
		{
			FixedWidthColumn fwc = (FixedWidthColumn)o;
			return fwc.FixedWidth;
		}
	}
}
